<!DOCTYPE html>
<html>
  <head>
    <title><%= title %></title>
    <link rel="stylesheet" href="/stylesheets/style.css" />
  </head>
  <body>
    <h1><%= title %></h1>
    <p>Welcome to <%= title %></p>
    <p><span>请打开控制台查看输出</span></p>
    <script src="/js/qs.js"></script>
    <script>
      const { parse } = Qs
      const href = window.location.href
      const getPageQuery = () => {
        return parse(href.split('?')[1])
      }
      console.log('测试获取所有参数:', getPageQuery())
      const getUrlParams = key => {
        return (
          decodeURIComponent(
            (new RegExp(`[?|&]${key}=([^&;]+?)(&|#|;|$)`).exec(href) || [
              ,
              '',
            ])[1].replace(/\+/g, '%20')
          ) || null
        )
      }
      console.log('测试获取指定参数:', getUrlParams('search'))
    </script>
    <script>
      /* 设置cookie */
      const setCookie = (key, value, expires) => {
        let exDate = new Date() // 创建时间对象
        const startTime = exDate.getTime() //将当前时间转换成毫秒数
        const exTime = 24 * 60 * 60 * 1000 * expires //构建有效期间毫秒数
        exDate.setTime(startTime + exTime) //将截止有效期时间戳转换成时间对象
        // 字符串拼接cookie
        //默认情况下，cookie 在浏览器关闭时删除, 使用 path 参数告诉浏览器 cookie 的路径。默认情况下，cookie 属于当前页面(path=/)。
        //cookie 设置过期时间 （expires=）
        window.document.cookie =
          key + '=' + value + ';path=/;expires=' + exDate.toUTCString()
      }
      /* 获取cookie */
      const getCookie = params => {
        let cookieObj = null
        if (document.cookie.length === 0) return cookieObj
        let arr = document.cookie.split('; ') //通过分号连空格将字符串切割成数组
        arr.some(element => {
          let arr2 = element.split('=') // 通过=再次切割
          if (arr2[0] === params) {
            cookieObj = { key: arr2[0], value: arr2[1] }
            return true
          }
          return false
        })
        return cookieObj
      }

      /* 获取所有cookie */
      const getCookies = () => {
        let cookieObj = []
        if (document.cookie.length === 0) return cookieObj
        let array = document.cookie.split('; ') //通过分号连空格将字符串切割成数组
        cookieObj = array.map(item => {
          let arr2 = item.split('=') // 通过=再次切割
          return {
            key: arr2[0],
            value: arr2[1],
          }
        })
        return cookieObj
      }
      /* 清除指定cookie */
      const clearCookie = key => {
        setCookie(key, '', -1) // 将生命周期改为-1
      }

      /* 清除所有cookie */
      const clearCookies = () => {
        getCookies().forEach(element => {
          setCookie(element.key, '', -1) //将生命周期改为-1
        })
      }

      setCookie('userName', '琴时', 1)
      setCookie('age', '18', 1)
      console.log('测试获取cookie-userName:', getCookie('userName'))
      console.log('测试所有cookie:', getCookies())
      clearCookie('userName') //解开注释将清除cookie
      console.log('测试清除cookie：userName---------------------- ')
      console.log('测试所有cookie:', getCookies())
      console.log('测试清除所有cookie-----------------------------')
      clearCookies()
      console.log('测试所有cookie:', getCookies())
    </script>
  </body>
</html>
